﻿using System;
using System.Collections.Generic;
using System.Text;
using DTO;
using System.Data.SqlClient;
namespace DAO
{
    public class CHITIETHOADONDAO
    {
        ///CỦA AI THÌ TỰ ĐÓNG KHUNG VÀO. TRÁNH XÓA NHẦM CODE
        /////////////////CỦA LINH///////////////////
        public CHITIETHOADONDTO LayThongTinCTHD(HOADONDTO HDTO, THUCDONDTO TDTO)
        {
            SqlConnection conn = DataProvider.ConnectDB();

            SqlCommand command = new SqlCommand("LINH31_LAYTHONGTINCTHD", conn);
            command.CommandType = System.Data.CommandType.StoredProcedure;

            command.Parameters.Add("@MaHD", System.Data.SqlDbType.Int);
            command.Parameters["@MaHD"].Value = HDTO.MaHD;

            command.Parameters.Add("@MaMon", System.Data.SqlDbType.Int);
            command.Parameters["@MaMon"].Value = TDTO.MaMon;
            
            SqlDataReader reader = command.ExecuteReader();
            CHITIETHOADONDTO CDTO = new CHITIETHOADONDTO();
            if (reader.Read())
            {
                CDTO.MaHD = reader.GetInt32(0);
                CDTO.MaNV = reader.GetInt32(1);
                CDTO.MaMon = reader.GetInt32(2);
            }

            conn.Close();
            return CDTO;
        }
        public int GoiMon(CHITIETHOADONDTO CDTO)
        {
            SqlConnection conn = DataProvider.ConnectDB();

            SqlCommand command = new SqlCommand("LINH27_GOIMON", conn);
            command.CommandType = System.Data.CommandType.StoredProcedure;
            //GOIMON(@MaHD int, @MaMon int, @KhuyenMai bit, @MaNV int, @SoLuong int)
            command.Parameters.Add("@MaHD", System.Data.SqlDbType.Int);
            command.Parameters["@MaHD"].Value = CDTO.MaHD;

            command.Parameters.Add("@MaMon", System.Data.SqlDbType.Int);
            command.Parameters["@MaMon"].Value = CDTO.MaMon;

            command.Parameters.Add("@KhuyenMai", System.Data.SqlDbType.Int);
            command.Parameters["@KhuyenMai"].Value = CDTO.KhuyenMai;

            command.Parameters.Add("@MaNV", System.Data.SqlDbType.Int);
            command.Parameters["@MaNV"].Value = CDTO.MaNV;

            command.Parameters.Add("@SoLuong", System.Data.SqlDbType.Int);
            command.Parameters["@SoLuong"].Value = CDTO.SoLuong;

            int x = command.ExecuteNonQuery();
            conn.Close();
            return x;
        }
        public int HuyMon(CHITIETHOADONDTO CDTO, bool bit)
        {
            SqlConnection conn = DataProvider.ConnectDB();

            SqlCommand command = new SqlCommand("LINH28_HUYMON", conn);
            command.CommandType = System.Data.CommandType.StoredProcedure;
            //HUYMON(@MaHD int, @MaMon int)
            command.Parameters.Add("@MaHD", System.Data.SqlDbType.Int);
            command.Parameters["@MaHD"].Value = CDTO.MaHD;

            command.Parameters.Add("@MaMon", System.Data.SqlDbType.Int);
            command.Parameters["@MaMon"].Value = CDTO.MaMon;

            command.Parameters.Add("@Bit", System.Data.SqlDbType.Bit);
            command.Parameters["@Bit"].Value = bit;

            int x = command.ExecuteNonQuery();
            conn.Close();
            return x;
        }
        public int CapNhatThongTin(CHITIETHOADONDTO CDTOCu, CHITIETHOADONDTO CDTOMoi)
        {
            SqlConnection conn = DataProvider.ConnectDB();
            //DOIMON(@MaHD int, @MaNVCu int, @MaNVMoi int, @MaMonCu int, @MaMonMoi int, @SoLuong int)
            SqlCommand command = new SqlCommand("LINH29_DOIMON", conn);
            command.CommandType = System.Data.CommandType.StoredProcedure;

            command.Parameters.Add("@MaHD", System.Data.SqlDbType.Int);
            command.Parameters["@MaHD"].Value = CDTOCu.MaHD;

            command.Parameters.Add("@MaNVCu", System.Data.SqlDbType.Int);
            command.Parameters["@MaNVCu"].Value = CDTOCu.MaNV;

            command.Parameters.Add("@MaNVMoi", System.Data.SqlDbType.Int);
            command.Parameters["@MaNVMoi"].Value = CDTOMoi.MaNV;

            command.Parameters.Add("@MaMonCu", System.Data.SqlDbType.Int);
            command.Parameters["@MaMonCu"].Value = CDTOCu.MaMon;

            command.Parameters.Add("@MaMonMoi", System.Data.SqlDbType.Int);
            command.Parameters["@MaMonMoi"].Value = CDTOMoi.MaMon;

            command.Parameters.Add("@SoLuong", System.Data.SqlDbType.Int);
            command.Parameters["@SoLuong"].Value = CDTOMoi.SoLuong;

            command.Parameters.Add("@KhuyenMai", System.Data.SqlDbType.Int);
            command.Parameters["@KhuyenMai"].Value = CDTOMoi.KhuyenMai;

            int x = command.ExecuteNonQuery();
            conn.Close();
            return x;
        }
        public List<CHITIETHOADONDTO> CTHD(HOADONDTO HDTO)
        {
            SqlConnection conn = DataProvider.ConnectDB();

            SqlCommand command = new SqlCommand("LINH36_CHITIETHD", conn);
            command.CommandType = System.Data.CommandType.StoredProcedure;

            command.Parameters.Add("@MaHD", System.Data.SqlDbType.Int);
            command.Parameters["@MaHD"].Value = HDTO.MaHD;

            SqlDataReader reader = command.ExecuteReader();
            List<CHITIETHOADONDTO> ds = new List<CHITIETHOADONDTO>();
            while (reader.Read())
            {
                //MaNV, TenNV, TenMon, DVT, SoLuong, KhuyenMai, DonGia
                CHITIETHOADONDTO CDTO = new CHITIETHOADONDTO();
                CDTO.MaNV = reader.GetInt32(0);
                CDTO.TenNV = reader.GetString(1);
                CDTO.TenMon = reader.GetString(2);
                CDTO.DVT = reader.GetString(3);
                CDTO.SoLuong = reader.GetInt32(4);
                CDTO.KhuyenMai = reader.GetInt32(5);
                CDTO.DonGia = 0;// Convert.ToInt64(reader.GetSqlMoney(6));
                ds.Add(CDTO);
            }
            conn.Close();
            return ds;
        }
        ////////////////////////////////////////////
    }
}
